package com.atguigu.teach.mytask;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class MyNotSafeDemo {

    public static void listnotSafe(){
        /*不安全*/
        //List<String> list=new ArrayList<>();
        /*安全*/
        //List<String> list=new Vector<>();
        // List<String> list=Collections.synchronizedList(new ArrayList<>());
        List<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i <30 ; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0,6));
                System.out.println(list);
            },String.valueOf(i)).start();
        }
    }
    public static void main(String[] args) {
        /*不安全*/
        //List<String> list=new ArrayList<>();
        /*安全*/
        //List<String> list=new Vector<>();
        // List<String> list=Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList<Object> objects1 = new CopyOnWriteArrayList<>();
        for (int i = 0; i <30 ; i++) {
            new Thread(()->{
                objects1.add(UUID.randomUUID().toString().substring(0,6));
                System.out.println(objects1);
            },String.valueOf(i)).start();
        }
    }
}
